package com.nuanshui.heatedloan.baseservice.service;

import com.nuanshui.heatedloan.baseservice.dal.mapper.WeexVersionMapper;
import com.nuanshui.heatedloan.baseservice.facade.WeexVersionService;
import com.nuanshui.heatedloan.baseservice.form.WeexVersionForm;
import com.nuanshui.heatedloan.baseservice.vo.WeexVersionVO;
import com.nuanshui.heatedloan.constant.ServiceConstant;
import com.nuanshui.heatedloan.pageable.Pager;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service(ServiceConstant.WEEXVERSIONSERVICE)
public class WeexVersionServiceImpl implements WeexVersionService,InitializingBean {

	@Autowired
	private WeexVersionMapper weexVersionMapper;
	
	private Logger logger = LoggerFactory.getLogger(getClass());
	
	private static final int ANDROID = 0;
	private static final int IOS = 1;
	private static final String DEFAULT = "default";
	
	
	
	@Override
	public WeexVersionVO loadVO(String id) {
		return weexVersionMapper.loadVO(id);
	}

	@Override
	public void save(WeexVersionForm form) {
		weexVersionMapper.save(form);
		System.out.println(form.getPatchUrl());
		List<String> androidStr = form.getAndroidNativeVersionCode();
		List<String> iosStr = form.getIosNativeVersionCode();
		
		if(androidStr == null || androidStr.isEmpty()){
			weexVersionMapper.saveWeexClientNativeVersion(form.getId(), DEFAULT, ANDROID);
		}else{
			for(String str : androidStr){
				weexVersionMapper.saveWeexClientNativeVersion(form.getId(), str, ANDROID);
			}
		}
		if(iosStr == null || iosStr.isEmpty()){
			weexVersionMapper.saveWeexClientNativeVersion(form.getId(), DEFAULT, IOS);
		}else{
			for(String str :iosStr){
				weexVersionMapper.saveWeexClientNativeVersion(form.getId(), str, IOS);
			}
		}
	}

	@Override
	public Pager<WeexVersionVO> list(Pager<WeexVersionVO> pager) {
		List<WeexVersionVO> list = weexVersionMapper.list(pager);
		for(WeexVersionVO vo : list){
			//去获取当前weex对应的android 和ios的原生版本
			vo.setAndroidNativeVersionCode(weexVersionMapper.findWeexClientNativeVersionByWeexVersionId(vo.getId(), ANDROID));
			vo.setIosNativeVersionCode(weexVersionMapper.findWeexClientNativeVersionByWeexVersionId(vo.getId(), IOS));
			
		}
		pager.setData(list);
		return pager;
	}

	@Override
	public void delete(String id) {
		weexVersionMapper.delete(id);
	}

	@Override
	public void changeStatus(String id, Integer status) {
		weexVersionMapper.changeStatus(id, status);
	}

	@Override
	public WeexVersionVO loadWeexVersionByClientInfo(String nativeVersionCode, String jsVersionCode, Integer platForm) {
		if(StringUtils.isEmpty(nativeVersionCode)) nativeVersionCode = DEFAULT;
		WeexVersionVO vo = null;
		List<WeexVersionVO> list = weexVersionMapper.loadWeexVersionByClientInfo(nativeVersionCode, platForm);
		//可能会获取到多个，但是只获取一个
		if(list.size() > 0){
			if(list.size() > 1) logger.error("----------------------当前获取了一个以上----------------------");
			vo = list.get(0);
			vo.setUpdateResult(vo.getVersionCode().equals(jsVersionCode) ? 0 : 1);
			
		}else{
			vo = new WeexVersionVO();
			vo.setUpdateResult(0);//不更新
			vo.setVersionCode("-1");
		}
		return vo;
	}


	@Override
	public WeexVersionVO loadLatestWeexVersion(Integer platForm, String jsVersionCode){
		WeexVersionVO weexVersionVO = weexVersionMapper.loadLatestWeexVersion(platForm);
		if (weexVersionVO != null) {
			weexVersionVO.setUpdateResult(weexVersionVO.getAndroidNativeVersionCodeStr().equals(jsVersionCode) ? 0 : 1);//一定要返回1
			return weexVersionVO;
		}

		return weexVersionVO;
	}

	@Override
	public void afterPropertiesSet() throws Exception {

	}
}
